<!--
 * @Author: HeAo
 * @Date: 2021-12-30 08:33:04
 * @LastEditTime: 2022-01-05 19:05:20
 * @LastEditors: HeAo
 * @Description: 
 * @FilePath: \briup-webui-1group-teamwork\am-ui\src\pages\am\engineer\List.vue
 * 别乱动！
-->
<template>
  <div class="engineer">
    <!-- 按钮区域 -->
    <div class="btns">
      <el-form
        :inline="true"
        :model="params"
        size="small"
        class="demo-form-inline"
      >
        <el-form-item>
          <el-select
            v-model="params.charge_id"
            clearable
            placeholder="请选择区域经理"
          >
            <el-option
              v-for="(item, index) in charges"
              :key="index"
              :label="item.realname"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="params.customer_id"
            clearable
            placeholder="请选择客户"
          >
            <el-option
              v-for="(item, index) in cutomers"
              :key="index"
              :label="item.realname"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="toSearchHandler">查询</el-button>
        </el-form-item>
        <el-button
          @click="toAddHandler"
          style="float: right"
          type="primary"
          size="small"
          >添加</el-button
        >
      </el-form>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table size="small" :data="resData.list" style="width: 100%">
        <el-table-column type="expand">
          <template v-slot="{ row }">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="工程地址：">
                <span>{{ row.address }}</span>
              </el-form-item>
              <el-form-item label="工程经度：">
                <span>{{ row.longitude }}</span>
              </el-form-item>
              <el-form-item label="工程纬度：">
                <span>{{ row.latitude }}</span>
              </el-form-item>
              <el-form-item label="客户姓名：">
                <span>{{ row.customer_name }}</span>
              </el-form-item>
              <el-form-item label="客户电话：">
                <span>{{ row.customer_telephone }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column label="序号" type="index" width="60px" align="center">
        </el-table-column>
        <el-table-column
          label="工程编号"
          prop="serial_number"
          width="100px"
          align="center"
        >
        </el-table-column>
        <el-table-column label="工程名称" prop="name" align="center">
        </el-table-column>
        <el-table-column
          label="工程类型"
          prop="type"
          width="120px"
          align="center"
        >
        </el-table-column>
        <!-- <el-table-column
          label="开始时间"
          prop="create_time"
          width="120px"
          align="center"
        >
        </el-table-column>
        <el-table-column
          label="结束时间"
          prop="end_time"
          width="120px"
          align="center"
        >
        </el-table-column> -->
        <el-table-column
          label="区域经理"
          prop="charge_name"
          width="120px"
          align="center"
        >
        </el-table-column>
        <el-table-column label="工程状态" width="120px" align="center">
          <template v-slot="{ row }">
            <el-tag size="mini" v-if="row.status == '未绑定'" type="danger">{{
              row.status
            }}</el-tag>
            <el-tag
              size="mini"
              v-else-if="row.status == '检测中'"
              type="success"
              >{{ row.status }}</el-tag
            >
            <el-tag size="mini" v-else type="info">{{ row.status }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="160px" align="center">
          <template v-slot="{ row }">
            <el-button @click="toEditHandler(row)" size="mini" type="text"
              >编辑</el-button
            >
            <el-button
              :disabled="row.status == '已结束' ? true : false"
              @click="toBindHandler(row)"
              size="mini"
              type="text"
              >绑定</el-button
            >
            <el-button
              v-if="row.status == '已结束'"
              disabled
              @click="toEndHandler(row.id)"
              size="mini"
              type="text"
              style="color: grey"
              >结束</el-button
            >
            <el-button
              v-else
              @click="toEndHandler(row.id)"
              size="mini"
              type="text"
              style="color: red"
              >结束</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页组件区域 -->
    <el-pagination
      background
      style="margin-top: 1em"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="parseInt(resData.page)"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="parseInt(resData.pageSize)"
      layout="total, sizes, prev, pager, next, jumper"
      :total="parseInt(resData.total)"
    >
    </el-pagination>
    <!-- 模态框区域 -->
    <el-dialog
      @close="dialogCloseHandler"
      :title="title"
      :visible.sync="visible"
    >
      <el-form :model="form" :inline="true" :rules="rules" ref="engineerFrom">
        <el-form-item
          prop="name"
          label="工程名称"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          prop="serial_number"
          label="工程编号"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.serial_number" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          prop="address"
          label="工程地址"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.address" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          prop="serial_number"
          label="工程类型"
          :label-width="formLabelWidth"
        >
          <el-select v-model="form.type">
            <el-option label="工地" value="工地"></el-option>
            <el-option label="学校" value="学校"></el-option>
            <el-option label="园区" value="园区"></el-option>
            <el-option label="商场" value="商场"></el-option>
            <el-option label="写字楼" value="写字楼"></el-option>
            <el-option label="事业单位" value="事业单位"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          prop="longitude"
          label="工程经度"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.longitude" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          prop="latitude"
          label="工程纬度"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.latitude" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          prop="customer_name"
          label="客户姓名"
          :label-width="formLabelWidth"
        >
          <el-input
            :disabled="form.id ? true : false"
            v-model="form.customer_name"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          prop="customer_telephone"
          label="客户电话"
          :label-width="formLabelWidth"
        >
          <el-input
            :disabled="form.id ? true : false"
            v-model="form.customer_telephone"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          prop="charge_id"
          label="区域经理"
          :label-width="formLabelWidth"
        >
          <el-select v-model="form.charge_id">
            <el-option
              v-for="(item, index) in charges"
              :key="index"
              :label="item.realname"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="toSubmitHandler">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
//导入工具插件
import { get, postJSON } from "@/utils/request.js";
import _ from "lodash";
export default {
  data() {
    return {
      params: {
        page: 1,
        pageSize: 10,
      },
      charges: [], // 区域经理
      cutomers: [], // 客户
      resData: {},
      form: {},
      title: "", //模态框标题
      visible: false,
      formLabelWidth: "120px",
      rules: {
        name: [{ required: true, message: "请输入工程名称", trigger: "blur" }],
        serial_number: [
          { required: true, message: "请输入工程编号", trigger: "blur" },
        ],
        address: [
          { required: true, message: "请输入工程地址", trigger: "blur" },
        ],
        type: [
          { required: true, message: "请选择工程类型", trigger: "change" },
        ],
        longitude: [
          { required: true, message: "请输入工程经度", trigger: "blur" },
        ],
        latitude: [
          { required: true, message: "请输入工程纬度", trigger: "blur" },
        ],
        customer_name: [
          { required: true, message: "请输入客户姓名", trigger: "blur" },
        ],
        customer_telephone: [
          { required: true, message: "请输入客户电话", trigger: "blur" },
        ],
        charge_id: [
          { required: true, message: "请选择区域经理", trigger: "change" },
        ],
      },
    };
  },
  methods: {
    // 搜索按钮点击事件
    toSearchHandler() {
      this.pageQueryEngineers();
    },
    // 添加按钮点击事件
    toAddHandler() {
      // 表单置空
      this.form = {};
      // 修改标题
      this.title = "添加工程";
      // 打开模态框
      this.visible = true;
    },
    // 编辑按钮点击事件
    toEditHandler(row) {
      // 使用lodash深度克隆的方法，拿到row的独立拷贝，this.form和row就不会相互影响
      this.form = _.clone(row);
      this.visible = true;
    },
    // 绑定按钮点击事件
    toBindHandler(row) {
      //跳转至工程绑定详情页面
      //vue 路由的编程式导航
      this.$router.push({
        path: "Details",
        query: row,
      });
    },
    // 结束按钮点击事件
    toEndHandler(id) {
      this.$confirm("此操作将结束该工程, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        get("/engineer/finish", { id }).then((res) => {
          // 重载数据
          this.pageQueryEngineers();
          // 提示成功
          this.$message.success("工程结束成功");
        });
      });
    },
    //分页多条件查询工程信息
    pageQueryEngineers() {
      get("/engineer/pageQuery", this.params).then((res) => {
        // 将后台接口的响应结果，赋值给resData
        this.resData = res.data;
      });
    },
    //pageSize改变事件
    handleSizeChange(pageSize) {
      // 将分页插件最新的参数赋值给params
      this.params.pageSize = pageSize;
      // 调用查询设备的方法（数据重载）
      this.pageQueryEngineers();
    },
    // page改变事件
    handleCurrentChange(page) {
      this.params.page = page;
      this.pageQueryEngineers();
    },
    // 查询所有区域经理和客户
    findAllChargesAndCustomers() {
      // 查询区域经理
      get("/user/pageQuery", {
        page: 1,
        pageSize: 100,
        role_id: 3,
      }).then((res) => {
        this.charges = res.data.list;
      });
      // 查询客户
      get("/user/pageQuery", {
        page: 1,
        pageSize: 100,
        role_id: 4,
      }).then((res) => {
        this.cutomers = res.data.list;
      });
    },
    // 模态框关闭回调
    dialogCloseHandler() {
      this.$refs["engineerFrom"].clearValidate();
    },
    // 表单确定按钮点击事件
    toSubmitHandler() {
      postJSON("/engineer/saveOrUpdate", this.form).then((res) => {
        // 数据重载
        this.pageQueryEngineers();
        // 成功提示
        this.$message.success("操作成功");
        // 关闭模态框
        this.visible = false;
      });
    },
  },
  //vue生命周期钩子函数
  created() {
    //页面加载，调用查询工程数据的方法
    this.pageQueryEngineers();
    // 页面加载，调用查询区域经理、客户数据的方法
    this.findAllChargesAndCustomers();
  },
};
</script>
